package com.linkage.bss.crm.packagemgr.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.linkage.bss.commons.dao.Page;
import com.linkage.bss.crm.packagemgr.model.Area;
import com.linkage.bss.crm.packagemgr.model.Channel;
import com.linkage.bss.crm.packagemgr.smo.IPackageStatusSMO;
import com.linkage.bss.crm.packagemgr.util.DataGridManager;

/**
 *
 * 包状态管理类
 * 包启用，停用， 发布。
 *
 * @author  Administrator
 * @version  [版本号, 2013-5-10]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class PackageStatusAction extends DataGridManager {

	/**
	 * 注释内容
	 */
	private static final long serialVersionUID = 2731080358920510206L;

	private IPackageStatusSMO packageStatusSMO = null;

	public IPackageStatusSMO getPackageStatusSMO() {
		return packageStatusSMO;
	}

	public void setPackageStatusSMO(IPackageStatusSMO packageStatusSMO) {
		this.packageStatusSMO = packageStatusSMO;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	/**
	 * 停用
	 * @return [参数说明]
	 *
	 * @return String [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public String stopUsePackage() {
		HttpServletRequest request = ServletActionContext.getRequest();
		//停用
		String staffID = request.getParameter("staffId");
		String packageIdStr = request.getParameter("objId");
		if (packageIdStr == null || "".equals(packageIdStr)) {
			JSONObject retJsonObj = new JSONObject();
			retJsonObj.put("res", "-1");
			retJsonObj.put("resMsg", "包ID不能为空");
			result = retJsonObj;
			return SUCCESS;
		}
		Long packageId = Long.valueOf(packageIdStr);
		getPackageStatusSMO().stopUsePackage(packageId, Long.valueOf(staffID));
		JSONObject retJsonObj = new JSONObject();
		retJsonObj.put("res", "200");
		retJsonObj.put("resMsg", "停用成功");
		result = retJsonObj;

		// 记录操作日志

		return SUCCESS;
	}

	/**
	 * 启用
	 * @return [参数说明]
	 *
	 * @return String [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public String startUsePackage() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String staffID = request.getParameter("staffId");
		String packageIdStr = request.getParameter("objId");

		if (packageIdStr == null || "".equals(packageIdStr)) {
			JSONObject retJsonObj = new JSONObject();
			retJsonObj.put("res", "-1");
			retJsonObj.put("resMsg", "包ID不能为空");
			result = retJsonObj;
			return SUCCESS;
		}
		Long packageId = Long.valueOf(packageIdStr);
		try {
			getPackageStatusSMO().startUsePackage(packageId, Long.valueOf(staffID));
		} catch (Exception e) {
			JSONObject retJsonObj = new JSONObject();
			retJsonObj.put("res", "-1");
			retJsonObj.put("resMsg", e.getMessage());
			result = retJsonObj;
			return SUCCESS;
		}
		JSONObject retJsonObj = new JSONObject();
		retJsonObj.put("res", "200");
		retJsonObj.put("resMsg", "启用成功！");
		result = retJsonObj;

		return SUCCESS;
	}

	/**
	 * 活动发布、
	 *
	 */
	public String publishPackage() {
		HttpServletRequest request = ServletActionContext.getRequest();

		try {

			String packageID = request.getParameter("objId");
			String staffID = request.getParameter("staffId");

			String areaIds = request.getParameter("areaIds");
			List<Integer> areaList = new ArrayList<Integer>();
			if (areaIds != null && !areaIds.equals("")) {
				String[] areaId = areaIds.split(",");
				for (int i = 0; i < areaId.length; i++) {
					areaList.add(Integer.valueOf(areaId[i]));
				}
			}

			String channels = request.getParameter("channels");
			List<Integer> channelList = new ArrayList<Integer>();
			if (channels != null && !channels.equals("")) {
				channels = URLDecoder.decode(channels, "UTF-8");
				JSONArray channelArray = JSONArray.fromObject(channels);
				for (int i = 0; i < channelArray.size(); i++) {
					JSONObject obj = channelArray.getJSONObject(i);
					channelList.add(obj.getInt("channelId"));
				}
			}

			getPackageStatusSMO().publishPackage(Long.valueOf(packageID), areaList, channelList, Long.valueOf(staffID));

			JSONObject retJsonObj = new JSONObject();
			retJsonObj.put("result", true);
			result = retJsonObj;
		} catch (Exception e) {
			JSONObject retJsonObj = new JSONObject();
			retJsonObj.put("result", false);
			retJsonObj.put("resMsg", e.getMessage());
			result = retJsonObj;
		}
		return SUCCESS;
	}

	public String getAreaByName() throws UnsupportedEncodingException {

		// 封装查询条件
		String areaName = ServletActionContext.getRequest().getParameter("areaName");
		//
		String areaId = ServletActionContext.getRequest().getParameter("areaId3Level");
		Integer areaIdInt = Integer.valueOf(areaId);
		if (areaName != null) {
			areaName = URLDecoder.decode(areaName, "UTF-8");
		}

		int total = getPackageStatusSMO().getAreaCountByName(areaName, areaIdInt);

		Page queryPage = new Page();
		queryPage.setTotalSize(total);
		queryPage.setPageSize(rows);
		queryPage.init();
		queryPage.setCurPage(page);

		List<Area> areas = getPackageStatusSMO().getAreaByName(areaName, areaIdInt, queryPage);

		Map<String, Object> jsonMap = new HashMap<String, Object>();// 定义map
		jsonMap.put("total", total);// total键 存放总记录数，必须的
		jsonMap.put("rows", areas);// rows键 存放每页记录 list

		// json转化属性过滤，忽略掉值为null的属性
		result = JSONObject.fromObject(jsonMap, jsonConfig);// 格式化result
		// 一定要是JSONObject

		return SUCCESS;
	}

	public String getChannelByName() throws UnsupportedEncodingException {

		//封装查询条件
		String name = ServletActionContext.getRequest().getParameter("name");
		String publishAreaId = ServletActionContext.getRequest().getParameter("publishAreaId");
		Integer managerAreaId = Integer.valueOf(publishAreaId);
		//	String[] publishAreaIdList = publishAreaId.split(",");
		name = URLDecoder.decode(name, "UTF-8");

		int total = getPackageStatusSMO().getChannelByNameCount(name, managerAreaId);
		Page queryPage = new Page();
		queryPage.setTotalSize(total);
		queryPage.setPageSize(rows);
		queryPage.init();
		queryPage.setCurPage(page);

		List<Channel> channels = getPackageStatusSMO().getChannelByName(name, managerAreaId, queryPage);

		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map
		jsonMap.put("total", total);//total键 存放总记录数，必须的
		jsonMap.put("rows", channels);//rows键 存放每页记录 list

		//json转化属性过滤，忽略掉值为null的属性
		result = JSONObject.fromObject(jsonMap, jsonConfig);//格式化result   一定要是JSONObject

		return SUCCESS;
	}

	public String getPackage2Channel() {

		//封装查询条件
		String packageIdStr = ServletActionContext.getRequest().getParameter("packageId");
		Long packageId = Long.valueOf(packageIdStr);
		List<Channel> channels = getPackageStatusSMO().queryPackage2Channel(packageId);

		//json转化属性过滤，忽略掉值为null的属性
		//	result = JSONObject.fromObject(channels, jsonConfig);//格式化result   一定要是JSONObject

		arrayResult = JSONArray.fromObject(channels, jsonConfig);
		return SUCCESS;
	}

	public String getPackage2Area() {

		//封装查询条件
		String packageIdStr = ServletActionContext.getRequest().getParameter("packageId");
		Long packageId = Long.valueOf(packageIdStr);
		List<Area> areaList = getPackageStatusSMO().queryPackage2Area(packageId);

		//json转化属性过滤，忽略掉值为null的属性
		arrayResult = JSONArray.fromObject(areaList, jsonConfig);
		return SUCCESS;
	}
}
